package candidate;

import com.dai.FlowableFirstApplication;
import com.dai.base.FlowableVariable;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.beans.Transient;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Title:
 * @Description: 方法描述:候选人
 * @param null:
 */


@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FlowableFirstApplication.class)
public class Candidata {
    @Autowired
    private ProcessEngine processEngine;//流程实例
    @Autowired
    private RepositoryService repositoryService;//流程部署
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    /**
     *  @ClassName: Candidata
     *  @Description: 类描述:流程部署
     *  @date: 2025/7/6
     */
   @Test
   public void deployProcess() {
       Deployment deploy = repositoryService.createDeployment()
               .addClasspathResource("process/candidate/HolidayDemo.bpmn20.xml")
               .name("候选人流程部署")
               .deploy();
       System.err.println("部署的流程id是"+deploy.getId());
   }
   /**
    *  @ClassName: Candidata
    *  @Description: 类描述:启动流程
    *  @date: 2025/7/6
    */
   @Test
    public void startProcess(){
       String processKey = "HolidayDemo:1:a20f36d2-5a68-11f0-91d0-b025aa86e2c3";
       Map<String, Object> map = new HashMap<>();
       map.put("candidate1","zhangsan");
       map.put("candidate2","lisi");
       map.put("candidate3","wangwu");
       ProcessInstance processInstance = runtimeService.startProcessInstanceById(processKey, map);
       System.err.println("我启动的流程实例是"+processKey);
   }
   /**
    *  @ClassName: Candidata
    *  @Description: 类描述:根据候选人查询任务
    *  @date: 2025/7/6
    */
   @Test
    public void selectUserTask() {
       List<Task> zhangsan =
               taskService.createTaskQuery().taskCandidateUser("zhangsan").list();
       for (Task task : zhangsan) {
           System.err.println("我的任务是" + task.getName());
           System.out.println("我的任务id是" + task.getId());

       }
   }

       //任务拾取
       @Test
    public void claimTask(){
           List<Task> zhangsan = taskService.createTaskQuery().taskCandidateOrAssigned("zhangsan").list();
           for (Task task : zhangsan) {
               //拾取任务
               // taskService.claim(task.getId(),"zhangsan");
               //放弃任务
               // taskService.unclaim(task.getId());
           }
       }
       //任务完成
    @Test
    public void completeTask(){
        List<Task> zhangsan = taskService.createTaskQuery().taskCandidateOrAssigned("zhangsan").list();
        Map<String, Object> map = new HashMap<>();
        map.put("candidate4","xiaodai");
        map.put("candidate5","xiaodai2");
        for (Task task : zhangsan) {
            System.err.println("任务的id是"+task.getId());
            //完成任务

            taskService.complete(task.getId(),map);
        }
    }

    //指派审批人
    @Test
    public void assignTask(){
        List<Task> zhangsan = taskService.createTaskQuery().taskCandidateOrAssigned("zhangsan").list();
        for (Task task : zhangsan) {
            taskService.setAssignee(task.getId(),"小戴666");
        }
    }
    @Test
    public void a(){
        Map<String, Object> map = new HashMap<>();
        map.put("candidate4","xiaodai");
        map.put("candidate5","xiaodai2");
        map.put("candidate6","xiaodai3");
        taskService.complete("57f38294-5af1-11f0-bcc4-b025aa86e2c3",map);
    }
    //查看小戴的候选任务
    @Test
    public void selectTask(){
        List<Task> xiaodai = taskService.createTaskQuery().taskCandidateUser("xiaodai").list();
        for (Task task : xiaodai) {
            System.err.println("我的任务是"+task.getName());
            System.err.println("我的任务id是"+task.getId());
        }
    }
    // 小戴拾取任务并且完成
    @Test
    public void claimTaskAndComplete(){
        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("xiaodai").list();
        for (Task task : list) {
            //拾取任务
            taskService.claim(task.getId(),"xiaodai");
           //完成任务
            taskService.complete(task.getId());
        }
    }

}
